package dper.study.codes.hbase.service.impl;

import com.alibaba.fastjson.JSONObject;
import dper.study.codes.elasticsearch.model.GitRepoModel;
import dper.study.codes.hbase.api.HGitRepoIndexService;
import dper.study.codes.hbase.consts.GitRepoConsts;
import dper.study.codes.hbase.util.RowKeyUtil;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.TableCallback;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leichen on 2017/4/14.
 */
@Service("hGitRepoIndexServiceImpl")
public class HGitRepoIndexServiceImpl implements HGitRepoIndexService {
    private Logger logger = Logger.getLogger(HGitRepoIndexServiceImpl.class) ;

    @Autowired
    private HbaseTemplate hbaseTemplate ;

    public int insertDocs(List<GitRepoModel> gitRepoModelList) {
        if(gitRepoModelList == null || gitRepoModelList.size()==0)
            return 0 ;
        logger.info(gitRepoModelList);
        final List<GitRepoModel> gitRepoModelListFinal = gitRepoModelList ;
        int count = hbaseTemplate.execute(GitRepoConsts.TABLE_NAME, new TableCallback<Integer>() {
            public Integer doInTable(HTableInterface hTableInterface) throws Throwable {
                List<Put> putList = new ArrayList<Put>() ;
                for(GitRepoModel gitRepoModel:gitRepoModelListFinal){
                    // rowKey => authorId + updateTime
                    // rowKey这种情况举例不适合 ， 以后修改为针对 CommitModel（提交信息)的rowKey 。 todo
                    String rowKey = RowKeyUtil.buildGRRowKey(gitRepoModel.getAuthorId()
                            ,gitRepoModel.getUpdateTime()) ;
                    Put put = new Put(rowKey.getBytes()) ;
                    put.addColumn(Bytes.toBytes(GitRepoConsts.FAMILY_GITREPO_INFO),
                            Bytes.toBytes(GitRepoConsts.COLUMN_GITREPO_CONTENT),
                            Bytes.toBytes(JSONObject.toJSONString(gitRepoModel))) ;
                    putList.add(put) ;

                    // rowkey  =>  gitId
                    String rowKeyGId = RowKeyUtil.buildGRRowKey(gitRepoModel.getId()) ;
                    Put putGid = new Put(rowKeyGId.getBytes()) ;
                    put.addColumn(Bytes.toBytes(GitRepoConsts.FAMILY_GITREPO_INFO),
                            Bytes.toBytes(GitRepoConsts.COLUMN_GITREPO_CONTENT),
                            Bytes.toBytes(JSONObject.toJSONString(gitRepoModel))) ;
                    putList.add(putGid) ;
                }
                hTableInterface.put(putList);
                return putList.size();
            }
        });
        return count;
    }

    public static void main(String[] argv){
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-hbase-*.xml") ;
        HGitRepoIndexService hGitRepoIndexService = ac.getBean("hGitRepoIndexServiceImpl",HGitRepoIndexService.class) ;
        List<GitRepoModel> gitRepoModelList = new ArrayList<GitRepoModel>() ;
        GitRepoModel gitRepoModel = new GitRepoModel() ;
        gitRepoModel.setId(100);
        gitRepoModel.setRepoName("dper-study-codes");
        gitRepoModelList.add(gitRepoModel) ;
        int retCount = hGitRepoIndexService.insertDocs(gitRepoModelList) ;
        System.out.println(retCount);
    }
}
